package com.rich.paas.controller;


import com.alibaba.fastjson.JSON;
import com.rich.paas.constant.Constant;
import com.rich.paas.enums.ResultCodeEnum;
import com.rich.paas.exception.BusinessException;
import com.rich.paas.namespace.RedisNamespace;
import com.rich.paas.domain.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * BaseController
 *
 * @author chenhao
 */
public class BaseController implements Constant,RedisNamespace {

    /**
     * logger
     */
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 手机号检验正则
     */
    protected final static String REGEX_MOBILE = "^((13[0-9])|145|147|17[0-9]|(15[^4\\D])|(18[0-9]))\\d{8}$";

    /**
     * 统一异常处理
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler
    public Result exp(HttpServletRequest request, Exception e) {
        logger.error("exception occurred：{},params:{}", e.getMessage(), JSON.toJSONString(request.getParameterMap()), e);
        if (e instanceof IllegalArgumentException) {
            return Result.failed(e.getMessage(), ResultCodeEnum.INVALID_PARAMETER.flag);
        }
        if (e instanceof BusinessException) {
            return Result.failed(e.getMessage());
        }
        return Result.failed(ERROR_MESSAGE, ResultCodeEnum.ERROR.flag);
    }

    /**
     * 后台系统用户登录成功后 写入到session登录信息
     * @param userId
     */
    protected void baUserLoginSuccess(Long userId){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        request.getSession().setAttribute(Constant.LOGIN_BA_USER_INFO, userId);
    }

    /**
     * 前端用户登录成功后 写入到session登录信息
     * @param userId
     */
    protected void frontUserLoginSuccess(Long userId){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        request.getSession().setAttribute(Constant.LOGIN_FRONT_USER_INFO, userId);
    }

    /**
     * 获取当前登录用户id
     *
     * @return
     */
    protected Long getCurrentFrontUserId()  {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Object userInfo = request.getSession().getAttribute(Constant.LOGIN_FRONT_USER_INFO);
        if (userInfo == null) {
            return null;
        }
        return Long.parseLong(userInfo.toString());
    }

    /**
     * 获取当前登录企业id
     *
     * @return
     */
    protected Long getCurrentBaUserId()  {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Object userInfo = request.getSession().getAttribute(Constant.LOGIN_BA_USER_INFO);
        if (userInfo == null) {
            return null;
        }
        return Long.parseLong(userInfo.toString());
    }
}